<?php
$years = $this->getYears($this->cycle);
$months = $this->getMonths();
$totales = array();
?>
<script>
    function hide(classId, name, tri){
        if(typeof tri !== 'undefined'){
            var triColspan = parseInt($("." + tri + "").attr('colspan'));
            var newTriColspan = triColspan - 2;
            $("." + tri + "").attr('colspan', newTriColspan);
            if(newTriColspan == 0){
                $("." + tri +"").hide();
            }
        }
        var element1 = "<span id=\"" + name + "\"><a onclick=\"show('" + classId + "','" + name + "')\">" + name + "</a>&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        var element2 = "<span id=\"" + name + "\"><a onclick=\"show('" + classId + "','" + name + "','" + tri + "')\">" + name + "</a>&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        $("." + classId + "").hide();
        var append = typeof tri !== 'undefined' ? element2 : element1;
        $("#mostrar").append(append);
    }
    
    function show(classId, name, tri){
        $("." + classId + "").show();
        if(typeof tri !== 'undefined'){
            var triColspan = parseInt($("." + tri +"").attr('colspan'));
            var newTriColspan = triColspan + 2;
            $("." + tri +"").attr('colspan', newTriColspan);
            if(newTriColspan == 2){
                $("." + tri +"").show();
            }
        }
        if(classId == "nombre" || classId == "empresa" || classId == "nit"){
            var currColspan = parseInt($(".totalMes").attr('colspan'));
            var newColspan = currColspan + 1;
            $(".totalMes, .totalTri").attr('colspan', newColspan);
        }
        $("#" + name + "").remove();
    };
    function viewFactura(id){
        URL="../facturasbrowser/view.php?fromReport=true&id="+id;
        reporte = window.open(URL,'_blank', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=550,height=500,left = 0,top = 0');
    }
</script>
<div id='mostrarCont'>
    <table style="width: 30%" class="table table-bordered table-striped">
        <tr>
            <th style="text-align: left;font-size:10px" colspan="20" width="5%" style="color:">Mostrar:&nbsp;<a onclick="$('#doreporte').trigger('click');">Todos</a></th>
        </tr>
        <tr>
            <td id="mostrar" style="text-align: left;font-size:10px">
            </td>
        </tr>
    </table>
</div>
<?php
foreach ($this->career as $c){
    $career = $this->db->query_select("cllg_career",sprintf("ID='%s'", $c['ID']));
    if($c['ID'] == -1){
        $students = $this->getStudents($c['ID'], $this->cycle, 'inact');
    }else{
        $students = $this->getStudents($c['ID'], $this->cycle);
    }
    foreach ($years as $year) {
        $index = 1;
        foreach($months as $month){
            $totales[$month['mest'] . $year] = 0;
        }
        $totales["ins" . $year] = 0;
        while (count($months) > $index){
            $totales["tri" . $months[$index]['tri'] . $year] = 0;
            $index += 3;
        }
    }
    
    if(count($students) == 0){
        if(getParam("career") != 'all'){
        ?>
        <span style="color:blue">No se encontraron alumnos asignados en este ciclo y carrera!!</span>
        <?php
        }
    }else{
        if($c['ID'] != -1){
        ?>
        <legend style="font-size: 16px; padding-top: 0px; margin-top: 0px"><?php echo $career[0]['NAME']?></legend>
        <?php
        }else{
            ?>
            <legend style="font-size: 16px; padding-top: 0px; margin-top: 0px">Alumnos no inscritos este ciclo</legend>
            <?php
        }
        ?>
        <table style="table-layout: fixed" class="table table-bordered table-striped table-hover">
            <tr>
                <th style="width: 100px" rowspan="2">Carnet</th>
                <th rowspan="2" style="width: 170px; overflow: hidden" class="nombre"><a onclick="hide('nombre','Nombre'); $('.totalMes,.totalTri').attr('colspan', $('.totalMes').attr('colspan') - 1);">Nombre</a></th>
                <th style="width: 100px" rowspan="2" class="empresa"><a onclick="hide('empresa','Empresa'); $('.totalMes').attr('colspan', $('.totalMes, .totalTri').attr('colspan') - 1);">Empresa</a></th>
                <th style="width: 100px" rowspan="2" class="nit"><a onclick="hide('nit','Nit'); $('.totalMes').attr('colspan', $('.totalMes, .totalTri').attr('colspan') - 1);">Nit</a></th>
                <?php
                foreach($years as $year){
                    ?>
                    <th style="width: 200px" colspan="2" class="inscripciones_<?php echo $year?>"><a onclick="hide('inscripciones_<?php echo $year?>','Inscripciones_<?php echo $year?>')">Inscripciones<?php echo " " . $year?></a></th>
                    <?php
                    foreach($months as $month){
                        ?>
                        <th style="width: 200px" colspan="2" class="<?php echo $month['mest']?>_<?php echo $year?>"><a onclick="hide('<?php echo $month['mest']?>_<?php echo $year?>','<?php echo $month['mest']?>_<?php echo $year?>','<?php echo "tri" . $month['tri'] . "_" . $year?>')"><?php echo $month['mest']. " " . $year?></a></th>
                        <?php
                    }
                }
                ?>
                <th style="width: 70px" rowspan="2">Total Pagado</th>
                <th style="width: 70px" rowspan="2">Total Pendiente</th>
            </tr>
            <tr>
                <?php
                foreach($years as $year){
                    ?>
                    <th style="width: 100px" class="inscripciones_<?php echo $year?>">Facturas</td>
                    <th style="width: 100px" class="inscripciones_<?php echo $year?>">Cuota</td>
                    <?php
                    foreach($months as $month){
                        ?>
                        <th style="width: 100px"  class="<?php echo $month['mest']?>_<?php echo $year?>">Facturas</td>
                        <th style="width: 100px"  class="<?php echo $month['mest']?>_<?php echo $year?>">Cuota</td>
                        <?php
                    }
                }
                ?>
            </tr>
        <?php
        $sumaPendiente = 0;
        $sumaPagado = 0;
        foreach($students as $s){
            $totalPagado = 0;
            $stat = $this->getStatus($s['ID'], $c['ID'], $this->cycle);
            ?>
            <tr>
                <td><?php echo $s['ID']?></td>
                <td class="nombre" style="<?php echo ( !$stat ? "text-decoration:line-through" : "") ?>"><?php echo $s['FIRST_NAME'] . " " . $s['LAST_NAME']?></td>
                <?php
                if($s['FK_EMPRESA'] != NULL){
                    $emp = $this->db->query_select("cllg_empresa", sprintf("ID='%s'", $s['FK_EMPRESA']));
                    $emp = $emp[0];
                }
                ?>
                <td class="empresa"><?php echo ($s['FK_EMPRESA'] != NULL ? $emp['NOMBRE'] : "&nbsp;");?></td>
                <?php
                $nit = $this->getNit($s['ID']);
                ?>
                <td class="nit"><?php echo $nit; ?></td>
                <?php
                foreach($years as $year){
                    $ins = $this->getIns($this->cycle, $c['ID'], $s['ID'], $year);
                    $facturasIns = count($ins) > 0 ? $this->getFacturas($ins) : 0;
                    ?>
                    <td class="inscripciones_<?php echo $year?>">
                        <?php
                        if($facturasIns != 0){
                            $i = 0;
                            $montoTotalIns = $this->getMontoPagado($ins);
                            $totales["ins" . $year] += $montoTotalIns;
                            $totalPagado += $montoTotalIns;
                            foreach ($facturasIns as $fi) {
                                $strFactsIns = "";
                                $facturaIns = $this->getFactura($fi['FK_FACTURA']);
                                if($facturaIns != 0){
                                    $facturaIns = $facturaIns[0];
                                    $style = $facturaIns['STATUS'] == 2 ? "color:red" : "";
                                    $strFactsIns.='<a style="'. $style . '" href="javascript:;" onclick="viewFactura(' . $facturaIns["ID"] . ')">' . $facturaIns["SERIE"] . "-" . $facturaIns["NUMERO"] . '</a>';
                                    $i++;
                                    $strFactsIns.= $i == 2 ? '<br/>' : '&nbsp;';
                                    $i = $i == 2 ? 0 : 1;
                                    echo $strFactsIns;
                                }
                            }
                            unset($i);
                        }  
                        ?>
                    </td>
                    <td class="inscripciones_<?php echo $year?>"><?php echo ($facturasIns != 0 ? "<span style='color:blue'>". number_format($montoTotalIns, 2) ."</span>" : "<span style='color:blue'>0.00</span>"); ?></td>
                    <?php
                    foreach($months as $month){
                        $cargos = $this->getCargo($this->cycle, $c['ID'], $s['ID'], $month['mes'], $year);
                        $facturas = count($cargos) > 0 ? $this->getFacturas($cargos) : 0;
                        ?>
                        <td class="<?php echo $month['mest']?>_<?php echo $year?>">
                            <?php
                            if($facturas != 0){
                                $i = 0;
                                $montoTotal = $this->getMontoPagado($cargos);
                                $totales[$month['mest'] . $year] += $montoTotal;
                                $totalPagado += $montoTotal;
                                $totales["tri" . $month['tri'] . $year] += $montoTotal;
                                foreach ($facturas as $f) {
                                    $strFacts = "";
                                    $factura = $this->getFactura($f['FK_FACTURA']);
                                    if($factura != 0){
                                        $factura = $factura[0];
                                        $style = $factura['STATUS'] == 2 ? "color:red" : "";
                                        $strFacts.='<a style="'. $style . '" href="javascript:;" onclick="viewFactura(' . $factura["ID"] . ')">' . $factura["SERIE"] . "-" . $factura["NUMERO"] . '</a>';
                                        $i++;
                                        $strFacts.= $i == 2 ? '<br/>' : '&nbsp;';
                                        $i = $i == 2 ? 0 : 1;
                                        echo $strFacts;
                                    }
                                }
                                unset($i);
                            }
                            ?>
                        </td>
                        <td class="<?php echo $month['mest']?>_<?php echo $year?>"><?php echo ($facturas != 0 ? "<span style='color:blue'>". number_format($montoTotal, 2) ."</span>" : "<span style='color:red'>0.00</span>"); ?></td>
                        <?php
                    }
                }
                ?>
                <th><?php echo "<span style='color:blue;font-weight:bolder'>" . number_format($totalPagado, 2) . "</span>" ?></th>
                <th><?php echo "<span style='color:red;font-weight:bolder'>" . number_format($this->getPendiente($s['ID']), 2) . "</span>"?></th>
                <?php 
                $sumaPendiente += $this->getPendiente($s['ID']);
                $sumaPagado += $totalPagado;
                ?>
            </tr>
            <?php
        }
        ?>
            <tr>
                <th colspan="4" style="text-align: right" class="totalMes">Totales por mes</th>
                <?php
                foreach($years as $year){
                    ?>
                    <th class="inscripciones_<?php echo $year?>">Inscripciones</th>
                    <th class="inscripciones_<?php echo $year?>"><?php echo "<span style='color:blue;font-weight:bolder'>" . number_format($totales["ins". $year],2) . "</span>" ?></th>
                    <?php
                    foreach($months as $month){
                        ?>
                        <th class="<?php echo $month['mest']?>_<?php echo $year?>"><?php echo $month['mest']. " " . $year?></th>
                        <th class="<?php echo $month['mest']?>_<?php echo $year?>"><?php echo "<span style='color:blue;font-weight:bolder'>" . number_format($totales[$month['mest'] . $year],2) . "</span>" ?></th>
                        <?php
                    }
                }
                ?>
                <th><?php echo "<span style='color:blue;font-weight:bolder'>" . number_format($sumaPagado,2) . "</span>" ?></th>
                <th><?php echo "<span style='color:red;font-weight:bolder'>" . number_format($sumaPendiente,2) . "</span>" ?></th>
            </tr>
            <tr>
                <th colspan="4" style="text-align: right" class="totalMes">Totales por timestre</th>
                <?php
                
                foreach ($years as $year) {
                    $index = 1;
                    ?>
                    <th colspan="2" class="inscripciones_<?php echo $year?>">&nbsp;</th>
                    <?php
                
                    while (count($months) > $index){
                        ?>
                    <th colspan="6" style="text-align:center" class="<?php echo "tri" . $months[$index]['tri'] . "_" . $year?>"><span><?php echo "Trimestre " . $months[$index]['tri'] . ":&nbsp;&nbsp;"?></span><span style="color:blue"><?php echo number_format($totales["tri" . $months[$index]['tri'] . $year], 2)?></span></th>
                        <?php
                        $index += 3;
                    }
                }
                ?>
                <td colspan="2">&nbsp;</td>
            </tr>
        </table>
        <?php
    }
}
?>
